﻿using System;
using System.Data;
using System.Collections.Generic;
using CrazyTalk.Caching;
using CrazyTalk.DALFactory;
using CrazyTalk.IDAL;
using CrazyTalk.Models;
namespace CrazyTalk.BLL
{
    /// <summary>
    /// 1
    /// </summary>
    public class UserSessionBLL
    {
        private static UserSessionBLL _instance;
        private static readonly object SyncLock = new object();
        public static UserSessionBLL Instance
        {
            get
            {
                lock (SyncLock)
                {
                    if (_instance == null)
                    {
                        _instance = new UserSessionBLL();
                    }
                }
                return _instance;
            }
        }
        private readonly IUserSessionDAL dal = DataAccess.Get<IUserSessionDAL>();
        public UserSessionBLL()
        { }
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int RowID)
        {
            return dal.Exists(RowID);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(UserSession model)
        {
            return dal.Add(model);
        }
        /// <summary>
        /// 增加一条数据到缓存
        /// </summary>
        public void AddCache(UserSession model)
        {
            DefaultCache cache = DefaultCache.GetCacheService();
            string cacheKey = CacheKeys.mgr_User_UserSessionByUserToken + model.UserToken;//"/User/UserSession/" + model.UserToken;
            object oSession = cache.RetrieveObject(cacheKey);
            if (oSession != null)
            {
                //如果存在记录,则先移除
                cache.RemoveObject(cacheKey);
            }
            cache.AddObject(cacheKey, model);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(UserSession model)
        {
            return dal.Update(model);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int RowID)
        {
            return dal.Delete(RowID);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string RowIDlist)
        {
            return dal.DeleteList(RowIDlist);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserSession GetModel(string userToken)
        {
            return dal.GetModel(userToken);
        }
        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public UserSession GetModelByCache(string userToken)
        {
            DefaultCache cache = DefaultCache.GetCacheService();
            string cacheKey = CacheKeys.mgr_User_UserSessionByUserToken + userToken; //"/User/UserSession/" + userToken;
            UserSession session = cache.RetrieveObject(cacheKey) as UserSession;
            if (session == null)
            {
                session = dal.GetModel(userToken);
                if (session != null)
                {
                    cache.AddObject(cacheKey, session);
                }
            }
            return session;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<UserSession> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<UserSession> DataTableToList(DataTable dt)
        {
            List<UserSession> modelList = new List<UserSession>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                UserSession model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new UserSession();
                    if (dt.Rows[n]["RowID"] != null && dt.Rows[n]["RowID"].ToString() != "")
                    {
                        model.RowID = int.Parse(dt.Rows[n]["RowID"].ToString());
                    }
                    if (dt.Rows[n]["UserToken"] != null && dt.Rows[n]["UserToken"].ToString() != "")
                    {
                        model.UserToken = dt.Rows[n]["UserToken"].ToString();
                    }
                    if (dt.Rows[n]["UserID"] != null && dt.Rows[n]["UserID"].ToString() != "")
                    {
                        model.UserID = dt.Rows[n]["UserID"].ToString();
                    }
                    if (dt.Rows[n]["RoleID"] != null && dt.Rows[n]["RoleID"].ToString() != "")
                    {
                        model.RoleID = int.Parse(dt.Rows[n]["RoleID"].ToString());
                    }
                    if (dt.Rows[n]["IP"] != null && dt.Rows[n]["IP"].ToString() != "")
                    {
                        model.IP = dt.Rows[n]["IP"].ToString();
                    }
                    if (dt.Rows[n]["ActionType"] != null && dt.Rows[n]["ActionType"].ToString() != "")
                    {
                        model.ActionType = dt.Rows[n]["ActionType"].ToString();
                    }
                    if (dt.Rows[n]["StampTime"] != null && dt.Rows[n]["StampTime"].ToString() != "")
                    {
                        model.StampTime = DateTime.Parse(dt.Rows[n]["StampTime"].ToString());
                    }
                    if (dt.Rows[n]["Resource"] != null && dt.Rows[n]["Resource"].ToString() != "")
                    {
                        model.Resource = dt.Rows[n]["Resource"].ToString();
                    }
                    if (dt.Rows[n]["LastActiveTime"] != null && dt.Rows[n]["LastActiveTime"].ToString() != "")
                    {
                        model.LastActiveTime = DateTime.Parse(dt.Rows[n]["LastActiveTime"].ToString());
                    }
                    if (dt.Rows[n]["NickName"] != null && dt.Rows[n]["NickName"].ToString() != "")
                    {
                        model.NickName = dt.Rows[n]["NickName"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}
    }
}
